package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.domian.BaseInfo;
import org.jsbd.boss.domian.channel.FlowChartDto;
import org.jsbd.boss.domian.channel.FlowVo;
import org.jsbd.boss.domian.statistics.MiguUserStatisticsDTO;
import org.jsbd.boss.service.IFlowDataService;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/flow")
public class FlowDataController {

	private static final Logger logger = LoggerFactory.getLogger(FlowDataController.class);

	@Autowired
	private IFlowDataService flowDataService;

	/**
	 * 图表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list() {
		return "flow/list";
	}

	/**
	 * 图表数据
	 * 
	 * @param model
	 *            model
	 * @param flowVo
	 *            查询条件
	 * @return
	 */
	@RequestMapping(value = "listData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listData(Model model, FlowVo flowVo) {
		Map<String, Object> map = new HashMap<String, Object>();

		String startTime = flowVo.getStartTime();
		String endTime = flowVo.getEndTime();
		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
			String time = DateUtil.getHourBeforeDate(DateUtil.DATE_PATTEN_YY_MM_DD_HHMMSS, 2);
			flowVo.setStartTime(time.substring(0, 16));
			flowVo.setDay(time.substring(0, 10).replace("-", ""));
		} else {
			flowVo.setDay(startTime.substring(0, 10).replace("-", ""));
		}

		// String avg = flowDataService.getAvg(flowVo);
		List<String> xAxis = flowDataService.selectXAxis(flowVo);
		List<FlowChartDto> yAxis = flowDataService.selectYAxis(flowVo);

		// map.put("avg", avg);
		map.put("xAxis", xAxis);
		map.put("yAxis", yAxis);

		return map;
	}

	/**
	 * 表格列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/listGrid")
	public String listGrid() {
		return "flow/listGrid";
	}

	/**
	 * 流量数据
	 * 
	 * @param page
	 *            分页
	 * @param flowVo
	 *            查询条件
	 * @return
	 */
	@RequestMapping(value = "listGridJson", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listGridJson(DataGridModel page, FlowVo flowVo) {
//		String startTime = flowVo.getStartTime();
//		String endTime = flowVo.getEndTime();
//		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
//			String time = DateUtil.getY_M_DStr() + " 00:00:00";
//			flowVo.setStartTime(time.substring(0, 16));
//			flowVo.setDay(time.substring(0, 10).replace("-", ""));
//		} else {
//			flowVo.setDay(startTime.substring(0, 10).replace("-", ""));
//		}
		return flowDataService.findListGridJson(page, flowVo);
	}
	
	/**
	 * 大流量查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "/bigFlowList")
	public String bigFlowList() {
		return "flow/listFlow";
	}

	/**
	 * 大流量数据
	 * 
	 * @param model
	 *            model
	 * @param flowVo
	 *            查询条件
	 * @return
	 */
	@RequestMapping(value = "bigFlowData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> bigFlowData(Model model, FlowVo flowVo) {
		Map<String, Object> map = new HashMap<String, Object>();
		Integer type = flowVo.getType();
		String startTime = flowVo.getStartTime();
		String endTime = flowVo.getEndTime();
		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
			String time = DateUtil.getHourBeforeDate(DateUtil.DATE_PATTEN_YY_MM_DD_HHMMSS, 2);
			flowVo.setStartTime(time.substring(0, 16));
			flowVo.setDay(time.substring(0, 10).replace("-", ""));
		} else {
			flowVo.setDay(startTime.substring(0, 10).replace("-", ""));
		}

		List<String> xAxis = flowDataService.selectBigFlowXAxis(flowVo);
		List<Double> yAxis = new ArrayList<Double>();
		if (type == 1) {
			yAxis = flowDataService.selectBigFlowReceiveYAxis(flowVo);
		} else if (type == 2) {
			yAxis = flowDataService.selectBigFlowSendYAxis(flowVo);
		}

		map.put("xAxis", xAxis);
		map.put("yAxis", yAxis);

		return map;
	}

}
